package com.joesem.sale.client.logic.notice

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.joesem.sale.client.base.BaseFragment
import com.jiaxing.sale.client.databinding.FragmentNoticeBinding

class NoticeFragment : BaseFragment<FragmentNoticeBinding>() {
    private val fragments = mutableListOf(
        newInstance<NoticeOrderFragment>(),
        newInstance<NoticeEventFragment>()
    )
    private val titles = mutableListOf("工单", "服务")

    override fun FragmentNoticeBinding.initView(savedInstanceState: Bundle?) {

        vpNoticeContent.adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {

            override fun getItemCount() = fragments.size

            override fun createFragment(position: Int) = fragments[position]

        }
    }

    override fun FragmentNoticeBinding.initListener() {
        val tabAttach = TabLayoutMediator(
            tabNoticeTitle, vpNoticeContent, true
        ) { tab, position ->
            tab.text = titles[position]
        }
        tabAttach.attach()
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                tabAttach.detach()
            }
        })
    }
}